<?php

define('LIGHT_DEBUG', 1);

/**
 * APPLICATION
 */

$pd = trim(str_replace(array('/', '\\'), '/', dirname(__DIR__)), '/');
$dr = trim(str_replace(array('/', '\\'), '/', $_SERVER['DOCUMENT_ROOT']), '/');
$pu = trim(str_replace($dr, '', $pd), '/');

define('LIGHT_PROJECT_PATH', $pd);
define('LIGHT_PUBLIC_PATH', LIGHT_PROJECT_PATH . '/public');
define('LIGHT_LIBRARY_PATH', LIGHT_PROJECT_PATH . '/library');
define('LIGHT_APPLICATION_PATH', LIGHT_PROJECT_PATH . '/application');
define('LIGHT_LOG_PATH', LIGHT_APPLICATION_PATH . '/logs');

define('LIGHT_BASE_URL', '/' . $pu . '/public');
define('LIGHT_PROJECT_URL', 'http://' . $_SERVER['HTTP_HOST'] . LIGHT_BASE_URL);
define('LIGHT_ASSETS_URL', LIGHT_PROJECT_URL . '/assets');

/**
 * DB
 */

define('LIGHT_DB_ADAPTER', 'mysqli');
define('LIGHT_DB_HOST', 'localhost');
define('LIGHT_DB_USER', 'root');
define('LIGHT_DB_PASS', '');
define('LIGHT_DB_DATA', 'core');
define('LIGHT_DB_CHARSET', 'utf8');
define('LIGHT_DB_PROFILER', true);

/**
 * SESSION
 */

define('LIGHT_SESSION_NAME', 'LIGHT_CORE');

/**
 * LANGUAGE
 */

define('LIGHT_LANGUAGE_ENABLE', 0);
define('LIGHT_LANGUAGE_PATH', LIGHT_PROJECT_PATH . '/languages');
define('LIGHT_LANGUAGE_DEFAULT', 'bg');

/**
 * FILES
 */

define('LIGHT_FILES_UPLOAD_PATH', LIGHT_PROJECT_PATH . '/data/uploads');
define('LIGHT_FILES_MAX_SIZE', (5 * 1024 * 1024));

/**
 * CACHE
 */

define('LIGHT_CACHE_ENABLE', 0);
define('LIGHT_CACHE_PATH', LIGHT_APPLICATION_PATH . '/cache');
define('LIGHT_CACHE_LIFETIME', 3600);

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(LIGHT_APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

require 'Light/Log.php';
require 'Light/Exception.php';
require 'Light/Loader.php';

set_error_handler(array('Light_Exception', 'errorHandler'));

spl_autoload_register(array('Light_Loader', 'tryLoadClass'));

if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
    if (isset($_GET)) $_GET = Light_Util::arrayMapRecursive('stripslashes', $_GET);
    if (isset($_POST)) $_POST = Light_Util::arrayMapRecursive('stripslashes', $_POST);
    if (isset($_REQUEST)) $_REQUEST = Light_Util::arrayMapRecursive('stripslashes', $_REQUEST);
    if (isset($_COOKIE)) $_COOKIE = Light_Util::arrayMapRecursive('stripslashes', $_COOKIE);
}

/**
 * Base url
 */

Light_Core::$baseUrl = LIGHT_BASE_URL;

/**
 * DB
 */

Light_Core::$db = Light_Db::factory(LIGHT_DB_ADAPTER, array(
    'host'     => LIGHT_DB_HOST,
    'username' => LIGHT_DB_USER,
    'password' => LIGHT_DB_PASS,
    'dbname'   => LIGHT_DB_DATA,
    'profiler' => LIGHT_DB_PROFILER,
    'charset'  => LIGHT_DB_CHARSET
));

Light_Db_Table::setDefaultAdapter(Light_Core::$db);

/**
 * Cache
 */

if (LIGHT_CACHE_ENABLE) {

    $frontendOptions = array(
        'lifetime'                => LIGHT_CACHE_LIFETIME,
        'automatic_serialization' => true
    );

    $backendOptions = array(
        'cache_dir' => LIGHT_CACHE_PATH
    );

    Light_Core::$cache = Light_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

    Light_Db_Table::setDefaultMetadataCache(Light_Core::$cache);
}